//
//  RootViewController.m
//  MyOWNUITableView
//
//  Created by Jordy Heemskerk on 14-04-10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "RootViewController.h"
#import "OfferDetails.h"
#import "Product.h"
#import "XMLParser.h"
#import "MyOWNUITableViewAppDelegate.h"

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSArray *languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
	NSString *currentLanguage = [languages objectAtIndex:0];
	appDelegate = (MyOWNUITableViewAppDelegate *) [[UIApplication sharedApplication] delegate];
	[appDelegate setLanguage:currentLanguage];
	[languages release];
	[currentLanguage release];
	languages = nil;
	currentLanguage = nil;
	
	[self setTitle:NSLocalizedString(@"mainTitle", nil)];
	[self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"mapButton", nil)];
	[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"profileButton", nil)];
	
	
    appDelegate.products = [[NSMutableArray alloc] init];
	[NSThread detachNewThreadSelector:@selector(receiveProducts:) toTarget:self	withObject:nil];
		
}

- (void)receiveProducts:(id*) something {
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	 
	NSString *urlstr = [[[NSString alloc] initWithFormat:@"%@getProducts.php", [appDelegate baseURL]] autorelease];
	NSURL *url = [[[NSURL alloc] initWithString:urlstr] autorelease];
	NSXMLParser *xmlParser = [[[NSXMLParser alloc] initWithContentsOfURL:url] autorelease];
	
	XMLParser *parser = [[XMLParser alloc] initXMLParser];
	
	[xmlParser setDelegate:parser];
	
	BOOL succes = [xmlParser parse];
	
	if (!succes) {
		NSLog(@"Something went wrong with parsing the XML.. Bummer");
	}
	
	[self.tableView reloadData];
	
	[pool release];
	 
}
	 
- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}
/*
- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}
*/

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [appDelegate.products count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	Product *p = [appDelegate.products objectAtIndex:indexPath.row];
	
	cell.textLabel.text = [p name];
	[[cell imageView] setImage: [p image]];
	
	[p release];
	p = nil;

    return cell;
}




// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	OfferDetails *details = [[OfferDetails alloc] initWithNibName:@"OfferDetails" bundle:[NSBundle mainBundle]];
	Product *p = [appDelegate.products objectAtIndex:indexPath.row];
	details.product = p;
	[self.navigationController pushViewController:details animated:YES];
	[details release];
	details = nil;
}

- (void)dealloc {
    
	[super dealloc];
}


@end

